package com.guigusuqi.hy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.entity.CardType;
import com.guigusuqi.hy.entity.UserCard;
import com.guigusuqi.hy.entity.UserDeputyCard;
import com.guigusuqi.hy.service.CardTypeService;
import com.guigusuqi.hy.service.UserCardService;
import com.guigusuqi.hy.service.UserDeputyCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suqi
 * @since 2022-08-12
 */
@RestController
@Api(tags = {"用户-副卡管理"})
@RequestMapping("/hy/user-deputy-card")
public class UserDeputyCardController
{
    @Resource
    private UserDeputyCardService userDeputyCardService;

    @Resource
    private UserCardService userCardService;

    @Resource
    private CardTypeService cardTypeService;

    @ApiOperation("分享副卡")
    @GetMapping("/shareUserCard/{phone}/{userCardId}")
    public Result shareUserCard(@PathVariable @ApiParam(value = "分享给副卡用户的手机号") String phone,
                                @PathVariable @ApiParam(value = "主卡id") String userCardId)
    {
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(userCardId))
        {
            return Result.fail().message("参数有误");
        }
        // 通过主卡id找到对应的主卡
        UserCard userCard = userCardService.getById(userCardId);
        if (ObjectUtils.isEmpty(userCard))
        {
            return Result.fail().message("没有这张主卡!");
        }

        // 再通过卡类型与hy_card_type卡类型匹配，找出这张卡最大能分享副卡的数量
        String cardType = userCard.getCardType();
        LambdaQueryWrapper<CardType> q = new LambdaQueryWrapper<>();
        q.eq(CardType::getCardType,cardType);
        CardType cardType1 = cardTypeService.getOne(q);
        if (ObjectUtils.isEmpty(cardType))
        {
            return Result.fail().message("这张主卡卡类型设置错误!");
        }
        Integer cardMax = cardType1.getCardMax();

        LambdaQueryWrapper<UserDeputyCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDeputyCard::getUserCardId,userCardId);

        int count = userDeputyCardService.count(queryWrapper);
        if (count >= cardMax)
        {
            return Result.fail().message("分享的副卡大于"+cardMax+"张!");
        }
        List<UserDeputyCard> list = userDeputyCardService.list(queryWrapper);
        for (UserDeputyCard userDeputyCard : list)
        {
            if (StringUtils.equals(userDeputyCard.getPhone(),phone))
            {
                return Result.fail().message("不能重复分享");
            }
        }

        UserDeputyCard userDeputyCard = new UserDeputyCard();
        userDeputyCard.setPhone(phone);
        userDeputyCard.setUserCardId(userCardId);
        boolean save = userDeputyCardService.save(userDeputyCard);
        if (save)
        {
            return Result.success().message("分享成功!");
        }else
        {
            return Result.fail().message("分享失败!");
        }
    }
}

